<template>
<div>
    <div>
        <el-input
                class="addPosInput"
                size="mini"
                placeholder="添加职位..."
                prefix-icon="el-icon-plus"
                v-model="pos.name">
        </el-input>
        <el-button type="primary" icon="el-icon-plus" size="mini" @click="addPosition">添加</el-button>
    </div>
    <div>

        <el-table

                :data="positions"
                class="posManaMain"
                border
                stripe
                @selection-change="handleSelectionChange"
                size="mini"
                style="width: 70%">
            <el-table-column
                    type="selection"
                    width="55">

            </el-table-column>
            <el-table-column
                    prop="id"
                    label="编号"
                    width="55">
            </el-table-column>
            <el-table-column
                    prop="name"
                    label="职位名称"
                    width="180">
            </el-table-column>
            <el-table-column
                    prop="createDate"
                    label="创建时间"
            width="150">
            </el-table-column>
            <el-table-column
                    prop="enabled"
                    label="是否启用"
            width="150">
                    <template slot-scope="scope" >
                        <el-tag type="success" v-if="scope.row.enabled">已启用</el-tag>
                        <el-tag type="danger" v-else>未启用</el-tag>
                    </template>
            </el-table-column>
            <el-table-column label="操作">
                <template slot-scope="scope">
                    <el-button
                            size="mini"
                            @click="showEditView(scope.$index, scope.row)">编辑</el-button>
                    <el-button
                            size="mini"
                            type="danger"
                            @click="handleDelete(scope.$index, scope.row)">删除</el-button>
                </template>
            </el-table-column>
        </el-table>
        <el-button type="danger" size="mini" style="margin-top:8px" :disabled="multipleSelection.length==0" @click="deleteMany">批量删除</el-button>
    </div>
    <el-dialog
            title="修改职位"
            :visible.sync="dialogVisible"
            width="30%">
        <div>
            <el-tag>
                职位名称
            </el-tag>
            <el-input class="updatePosInput" size="mini" v-model="updatePos.name"></el-input>
        </div>
        <table class="enabledStyle">
            <tr>
                <td>
                    <el-tag>
                        是否启用
                    </el-tag>
                </td>
                <td>
                    <el-switch
                            style="margin-left: 10px;display: block"
                            v-model="updatePos.enabled"
                            active-text="启用"
                            inactive-text="禁用"
                            active-color="#13ce66"
                            inactive-color="#ff4949">
                    </el-switch>
                </td>
            </tr>


        </table>
        <span slot="footer" class="dialog-footer">
    <el-button @click="dialogVisible = false" size="mini">取 消</el-button>
    <el-button type="primary" @click="doUpdate" size="mini">确 定</el-button>
  </span>
    </el-dialog>
</div>

</template>

<script>
    export default {
        name: "PosMana",
        data(){
            return{
                pos:{
                    name:''
                },
                updatePos:{
                    name:'',
                    enabled:true,
                },
                dialogVisible:false,
                positions:[],
                multipleSelection:[]
            }
        },
        mounted() {
            this.initPosition();
        },
        methods:{
            deleteMany(){
                this.$confirm('是否删除【' +this.multipleSelection.length+ '】条记录？', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    let ids='?';
                    this.multipleSelection.forEach(item=>{
                        ids+='ids='+item.id+'&';
                    })
                    this.deleteRequest("/system/basic/pos/"+ids).then(resp=>{
                        if (resp){
                            this.initPosition();
                        }

                    })
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });
            },
            handleSelectionChange(val){
                this.multipleSelection=val;
            },
            showEditView(index,data){
                Object.assign(this.updatePos,data);
                this.dialogVisible = true;

            },
            doUpdate(index,data){
              this.putRequest("/system/basic/pos/",this.updatePos).then(resp=>{
                  if (resp){
                      this.initPosition();
                      this.dialogVisible=false;
                  }
              })
            },
            handleDelete(index,data){
                this.$confirm('是否删除【'+data.name+'】？', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    this.deleteRequest("/system/basic/pos/"+data.id).then(resp=>{
                        if (resp){
                            this.initPosition();
                        }

                    })
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });
            },
            addPosition(){
                if (this.pos.name){
                    this.postRequest("/system/basic/pos/",this.pos).then(resp=>{
                        if (resp){
                            this.initPosition();
                            this.pos.name='';
                        }
                    })
                }else {
                    this.$message.error('职位名称不可以为空，请重新输入！');

                }

            },
            initPosition(){
                this.getRequest("/system/basic/pos/").then(resp=>{
                    if (resp){
                        this.positions=resp;
                    }
                })
            }
        }

    }
</script>

<style>
    .enabledStyle{
        margin-top: 10px;
    }
    .updatePosInput{
        width: 200px;
        margin-left: 8px;
    }
    .addPosInput{
        width: 300px;
        margin-right:8px

    }
    .posManaMain{
        margin-top:8px;
    }

</style>